package com.cloud.mkz.admin.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.mkz.core.common.util.ResponseResult;
import com.cloud.mkz.core.log.annotation.SysLog;
import com.cloud.mkz.admin.api.entity.RbacRoleMenu;
import com.cloud.mkz.admin.service.RbacRoleMenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 角色权限关系
 *
 * @author mkz code generator
 * @date 2021-03-21 17:52:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rbac/rolemenu" )
@Api(value = "rbacrolemenu", tags = "角色权限关系管理")
public class RbacRoleMenuController {

    private final  RbacRoleMenuService rbacRoleMenuService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param rbacRoleMenu 角色权限关系
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin_rbacrolemenu_get')" )
    public ResponseResult getRbacRoleMenuPage(Page page, RbacRoleMenu rbacRoleMenu) {
        return ResponseResult.ok(rbacRoleMenuService.page(page, Wrappers.query(rbacRoleMenu)));
    }


    /**
     * 通过id查询角色权限关系
     * @param roleId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{roleId}" )
    @PreAuthorize("@pms.hasPermission('admin_rbacrolemenu_get')" )
    public ResponseResult getById(@PathVariable("roleId" ) Long roleId) {
        return ResponseResult.ok(rbacRoleMenuService.getById(roleId));
    }

    /**
     * 新增角色权限关系
     * @param rbacRoleMenus 角色权限关系
     * @return ResponseResult
     */
    @ApiOperation(value = "新增角色权限关系", notes = "新增角色权限关系")
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult save(@RequestBody List<RbacRoleMenu> rbacRoleMenus) {
        if(CollectionUtil.isEmpty(rbacRoleMenus)||rbacRoleMenus.get(0).getRoleId()==null){
            return ResponseResult.failed("数据错误");
        }
        rbacRoleMenuService.delteByRoleId(rbacRoleMenus.get(0).getRoleId());
        for (RbacRoleMenu rbacRoleMenu : rbacRoleMenus) {
            if(rbacRoleMenu.getMenuId()!=null){
                rbacRoleMenuService.save(rbacRoleMenu);
            }
        }
        return ResponseResult.ok(true, "保存成功");
    }

    /**
     * 修改角色权限关系
     * @param rbacRoleMenu 角色权限关系
     * @return R
     */
    @ApiOperation(value = "修改角色权限关系", notes = "修改角色权限关系")
    @SysLog("修改角色权限关系" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_rbacrolemenu_edit')" )
    public ResponseResult updateById(@RequestBody RbacRoleMenu rbacRoleMenu) {
        return ResponseResult.ok(rbacRoleMenuService.updateById(rbacRoleMenu));
    }

    /**
     * 通过id删除角色权限关系
     * @param roleId id
     * @return R
     */
    @ApiOperation(value = "通过id删除角色权限关系", notes = "通过id删除角色权限关系")
    @SysLog("通过id删除角色权限关系" )
    @DeleteMapping("/{roleId}" )
    @PreAuthorize("@pms.hasPermission('admin_rbacrolemenu_del')" )
    public ResponseResult removeById(@PathVariable Long roleId) {
        return ResponseResult.ok(rbacRoleMenuService.removeById(roleId));
    }

}
